package com.test.dao.impl;

import com.test.dao.OpenOrderDao;
import com.test.domain.OpenOrder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class OpenOrderDaoImpl implements OpenOrderDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<String> getOpenOrderNumListByPhoneNumberC(String customerPhoneNumber) {
        String sql="select distinct open_order_number  from open_order where customer_phone_number=?";
        return jdbcTemplate.queryForList(sql,String.class,customerPhoneNumber);
    }

    @Override
    public List<String> getOpenOrderNumListByPhoneNumberM(String merchantPhoneNumber) {
        String sql="select distinct open_order_number  from open_order where merchant_phone_number=?";
        return jdbcTemplate.queryForList(sql,String.class,merchantPhoneNumber);
    }

    @Override
    public OpenOrder getOpenOrderByOpenOrderNumber(String openOrderNumber) {
        System.out.println(openOrderNumber);
        String sql="select * from open_order where open_order_number=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(OpenOrder.class),openOrderNumber);
    }

    @Override
    public void updateSituationByMerchantPhoneNumberAndOrderNumber(String merchantPhoneNumber, String orderNumber) {
        String sql= "update open_order set order_situation=order_situation+1 where open_order_number=? and merchant_phone_number=?";
        jdbcTemplate.update(sql,orderNumber,merchantPhoneNumber);
    }
}
